﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace OnlineBattlemat
{
    public partial class UploadImageControl : UserControl
    {
        #region Private Fields
        OpenFileDialog m_FileBrowser = new OpenFileDialog();
        private BitmapImage m_Image;
        private bool? m_DialogResult;
        #endregion

        #region Constructor
        public UploadImageControl()
        {
            InitializeComponent();

            x_ErrorLabel.Text = string.Empty;
            x_DescriptionTextBox.BorderBrush = new SolidColorBrush(Colors.Black);

            x_BrowseButton.Focus();
        }
        #endregion

        #region Event Handlers
        private void x_TextBoxes_KeyDown(object sender, KeyEventArgs e)
        {
            x_ErrorLabel.Text = string.Empty;
            x_DescriptionTextBox.BorderBrush = new SolidColorBrush(Colors.Black);
            if (e.Key == Key.Enter)
            {
                x_UploadButton_Click(sender, new RoutedEventArgs());
            }
        }

        private void x_BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            //Updated for new multiple selection field in Silverlight 2.  -KL 2/20/09
            //m_FileBrowser.EnableMultipleSelection = false;
            m_FileBrowser.Multiselect = false;
            m_FileBrowser.Filter = "image Files (*.jpg;*.png)|*.jpg;*.png";
            m_DialogResult = m_FileBrowser.ShowDialog();

            //if (m_DialogResult == DialogResult.OK)
            if (m_DialogResult != null && m_DialogResult == true)
            {
                //Updated for newer Silverlight that has slightly different OpenFileDialog operation?  -KL 2/20/09
                //FileDialogFileInfo file = m_FileBrowser.SelectedFile;
                FileInfo file = m_FileBrowser.File;
                Stream imageStream = file.OpenRead();
                Image image = new Image();

                m_Image = new BitmapImage();
                m_Image.SetSource(imageStream);

                image.Source = m_Image;
                x_SelectedImageBorder.Child = image;

                x_DescriptionTextBox.Focus();
            }
        }

        private void x_UploadButton_Click(object sender, RoutedEventArgs e)
        {
            //if (String.IsNullOrEmpty(x_DescriptionTextBox.Text.Trim())) //I don't think we need to make a token name mandatory. ~KL 3/24/10
            //{
            //    x_ErrorLabel.Text = "Invalid Title";
            //    x_DescriptionTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
            //    x_DescriptionTextBox.Focus();
            //    return;
            //}

            //Clear out the image box
            (x_SelectedImageBorder.Child as Image).Source = null;

            //Updated for newer Silverlight that has slightly different OpenFileDialog operation?  -KL 2/20/09
            //if (m_DialogResult == DialogResult.OK)
            if (m_DialogResult != null && m_DialogResult == true)
            {
                Token token = new Token(x_DescriptionTextBox.Text, m_Image);
                Page.GetCurrentPage().Tokens.Add(token);
                Page.GetCurrentPage().AddTokenToPage(token, GetSize());
                //Add token to selectable tokens list...
            }

            x_DescriptionTextBox.Text = string.Empty;
            Page.GetCurrentPage().HideDialogs();
        }

        private void x_CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Page.GetCurrentPage().HideDialogs();
        }
        #endregion

        #region Private Methods
        private int GetSize()
        {
            if (x_Token1xRadioButton.IsChecked.Value)
            {
                return 1;
            }
            else if (x_Token2xRadioButton.IsChecked.Value)
            {
                return 2;
            }
            else
            {
                return 3;
            }
            //TODO: Make the size into an enum? Or remove the arbitrary M/L/H size restrictions...?
        }
        #endregion
    }
}